<?php
/*
 * Class that operate on table 'questionmaster'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-09-12 17:20
 */
class QuestionmasterMySqlDAO implements QuestionmasterDAO{


	public function post_decode($q){

		$q = str_replace("\\\"", "\"", $q);
		$q = str_replace("\\'", "'", $q);
		$q = str_replace("\\\\", "\\", $q);
		return $q;
	}
	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return QuestionmasterMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM questionmaster WHERE QuestionMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM questionmaster';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM questionmaster ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param questionmaster primary key
 	 */
	public function delete($QuestionMasterId){
		$sql = 'DELETE FROM questionmaster WHERE QuestionMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($QuestionMasterId);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param QuestionmasterMySql questionmaster
 	 */

	public function insert($questionmaster){



		$sql = 'INSERT INTO questionmaster (NestingLevel, CategoryMasterId, QuestionType, Question, Url, YesQuestionId, NoQuestionId, Diagnosis, FirstLink, IsTreeFinalised, MarkForDelete, ParentQuestionId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($questionmaster->nestingLevel);
		$sqlQuery->setNumber($questionmaster->categoryMasterId);
		$sqlQuery->set($questionmaster->questionType);
		$sqlQuery->set($this->post_decode($questionmaster->question));
		$sqlQuery->set($questionmaster->url);
		$sqlQuery->setNumber($questionmaster->yesQuestionId);
		$sqlQuery->setNumber($questionmaster->noQuestionId);
		$sqlQuery->set($this->post_decode($questionmaster->diagnosis));
		$sqlQuery->setNumber($questionmaster->firstLink);
		$sqlQuery->setNumber($questionmaster->isTreeFinalised);
		$sqlQuery->setNumber($questionmaster->markForDelete);
		$sqlQuery->setNumber($questionmaster->parentQuestionId);

		$id = $this->executeInsert($sqlQuery);	
		$questionmaster->id = $id;
		return $id;
	}
	

	/**
 	 * Update record in table
 	 *
 	 * @param QuestionmasterMySql questionmaster
 	 */
	public function update($questionmaster){
		$sql = 'UPDATE questionmaster SET NestingLevel = ?, CategoryMasterId = ?, QuestionType = ?, Question = ?, Url = ?, YesQuestionId = ?, NoQuestionId = ?, Diagnosis = ?, FirstLink = ?, IsTreeFinalised = ?, MarkForDelete = ?, ParentQuestionId = ? WHERE QuestionMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($questionmaster->nestingLevel);
		$sqlQuery->setNumber($questionmaster->categoryMasterId);
		$sqlQuery->set($questionmaster->questionType);
		$sqlQuery->set($this->post_decode($questionmaster->question));
		$sqlQuery->set($questionmaster->url);
		$sqlQuery->setNumber($questionmaster->yesQuestionId);
		$sqlQuery->setNumber($questionmaster->noQuestionId);
		$sqlQuery->set($this->post_decode($questionmaster->diagnosis));
		$sqlQuery->setNumber($questionmaster->firstLink);
		$sqlQuery->setNumber($questionmaster->isTreeFinalised);
		$sqlQuery->setNumber($questionmaster->markForDelete);
		$sqlQuery->setNumber($questionmaster->parentQuestionId);

		$sqlQuery->setNumber($questionmaster->questionMasterId);
		return $this->executeUpdate($sqlQuery);
	}
	
	
/**
 	 * Update record in table
 	 *
 	 * @param QuestionmasterMySql questionmaster
 	 */
	public function updateIsTreeFinalised($questionmaster,$status){
		$sql = 'UPDATE questionmaster SET IsTreeFinalised = ? WHERE FirstLink = ?';
		$sqlQuery = new SqlQuery($sql);
	
		$isTreeFinalised = -1;
		if($status){
			$isTreeFinalised = 1;	
		}else{
			$isTreeFinalised = 0;
		}
		
		$sqlQuery->setNumber($isTreeFinalised);
		
		$sqlQuery->setNumber($questionmaster);
		
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'TRUNCATE TABLE questionmaster';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByNestingLevel($value){
		$sql = 'SELECT * FROM questionmaster WHERE NestingLevel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryMasterId($value){
		$sql = 'SELECT * FROM questionmaster WHERE CategoryMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByQuestionType($value){
		$sql = 'SELECT * FROM questionmaster WHERE QuestionType = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByQuestion($value){
		$sql = 'SELECT * FROM questionmaster WHERE Question = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUrl($value){
		$sql = 'SELECT * FROM questionmaster WHERE Url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByYesQuestionId($value){
		$sql = 'SELECT * FROM questionmaster WHERE YesQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNoQuestionId($value){
		$sql = 'SELECT * FROM questionmaster WHERE NoQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDiagnosis($value){
		$sql = 'SELECT * FROM questionmaster WHERE Diagnosis = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFirstLink($value){
		$sql = 'SELECT * FROM questionmaster WHERE FirstLink = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIsTreeFinalised($value){
		$sql = 'SELECT * FROM questionmaster WHERE IsTreeFinalised = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMarkForDelete($value){
		$sql = 'SELECT * FROM questionmaster WHERE MarkForDelete = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByParentQuestionId($value){
		$sql = 'SELECT * FROM questionmaster WHERE ParentQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}
	
	
	public function queryByNestingLevelAndFirstLink($value,$value1){
		$sql = 'SELECT * FROM questionmaster WHERE NestingLevel = ? AND FirstLink = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		$sqlQuery->setNumber($value1);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryMasterIdAndQuestionType($value,$value1){
		$sql = 'SELECT * FROM questionmaster WHERE CategoryMasterId = ? AND QuestionType = ? AND IsTreeFinalised=1';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
                $sqlQuery->set($value1);
                return $this->getList($sqlQuery);
	}
	public function deleteByNestingLevel($value){
		$sql = 'DELETE FROM questionmaster WHERE NestingLevel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCategoryMasterId($value){
		$sql = 'DELETE FROM questionmaster WHERE CategoryMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByQuestionType($value){
		$sql = 'DELETE FROM questionmaster WHERE QuestionType = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByQuestion($value){
		$sql = 'DELETE FROM questionmaster WHERE Question = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUrl($value){
		$sql = 'DELETE FROM questionmaster WHERE Url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByYesQuestionId($value){
		$sql = 'DELETE FROM questionmaster WHERE YesQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNoQuestionId($value){
		$sql = 'DELETE FROM questionmaster WHERE NoQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDiagnosis($value){
		$sql = 'DELETE FROM questionmaster WHERE Diagnosis = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFirstLink($value){
		$sql = 'DELETE FROM questionmaster WHERE FirstLink = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIsTreeFinalised($value){
		$sql = 'DELETE FROM questionmaster WHERE IsTreeFinalised = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMarkForDelete($value){
		$sql = 'DELETE FROM questionmaster WHERE MarkForDelete = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByParentQuestionId($value){
		$sql = 'DELETE FROM questionmaster WHERE ParentQuestionId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return QuestionmasterMySql 
	 */
	protected function readRow($row){
		$questionmaster = new Questionmaster();
		
		$questionmaster->questionMasterId = $row['QuestionMasterId'];
		$questionmaster->nestingLevel = $row['NestingLevel'];
		$questionmaster->categoryMasterId = $row['CategoryMasterId'];
		$questionmaster->questionType = $row['QuestionType'];
		$questionmaster->question = $row['Question'];
		$questionmaster->url = $row['Url'];
		$questionmaster->yesQuestionId = $row['YesQuestionId'];
		$questionmaster->noQuestionId = $row['NoQuestionId'];
		$questionmaster->diagnosis = $row['Diagnosis'];
		$questionmaster->firstLink = $row['FirstLink'];
		$questionmaster->isTreeFinalised = $row['IsTreeFinalised'];
		$questionmaster->markForDelete = $row['MarkForDelete'];
		$questionmaster->parentQuestionId = $row['ParentQuestionId'];

		return $questionmaster;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return QuestionmasterMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>
